//
//  WCEditIncludeDirectoriesArrayController.m
//  WabbitCode
//
//  Created by William Towe on 4/10/09.
//  Copyright 2009 Revolution Software. All rights reserved.
//

#import "WCEditIncludeDirectoriesArrayController.h"
#import "WCEditIncludeDirectoriesTableView.h"
#import "WCIncludeDirectory.h"
#import "WCEditIncludeDirectoriesSheetController.h"


@implementation WCEditIncludeDirectoriesArrayController
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id < NSDraggingInfo >)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation {
	// don't allow drops on the row, only between rows
	if (operation == NSTableViewDropOn)
		return NSDragOperationNone;
	
	// if the pasteboard contains our special type it's going to be a move or nothing
	if ([[[info draggingPasteboard] types] containsObject:kWCEditIncludeDirectoriesTableViewPboardType]) {
		// we archived an NSIndexSet, unarchive and get the only index
		NSUInteger index = [[NSKeyedUnarchiver unarchiveObjectWithData:[[info draggingPasteboard] dataForType:kWCEditIncludeDirectoriesTableViewPboardType]] firstIndex];
		
		// don't allow a drop one row above and one row below the row being dragged
		if (index == row || index == --row)
			return NSDragOperationNone;
		return NSDragOperationMove;
	}
	
	// otherwise we have NSFilenamesPboardType on the pasteboard
	BOOL isDirectory = NO;
	NSSet *includeDirectoryPaths = [NSSet setWithArray:[[self arrangedObjects] valueForKey:@"directoryPath"]];
	// loop through each file path and perform some checks
	for (NSString *filePath in [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType]) {
		// if the file isn't a directory reject the entire drag
		if (![[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory] || !isDirectory)
			return NSDragOperationNone;
		// if the directory path already exists, reject the entire drag
		else if ([includeDirectoryPaths containsObject:filePath])
			return NSDragOperationNone;
	}
	return NSDragOperationCopy;
}

- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id < NSDraggingInfo >)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation {
	if ([[[info draggingPasteboard] types] containsObject:kWCEditIncludeDirectoriesTableViewPboardType]) {
		// we archived an NSIndexSet, unarchive and get the only index
		NSUInteger index = [[NSKeyedUnarchiver unarchiveObjectWithData:[[info draggingPasteboard] dataForType:kWCEditIncludeDirectoriesTableViewPboardType]] firstIndex];
		
		// insert the object being dragged at the new index
		[self insertObject:[[self arrangedObjects] objectAtIndex:index] atArrangedObjectIndex:row];
		
		// adjust the index
		if (row <= index)
			++index;
		
		// remove the old object
		[self removeObjectAtArrangedObjectIndex:index];
	}
	else {
		for (NSString *filePath in [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType]) {
			[self insertObject:[WCIncludeDirectory includeDirectoryWithDirectoryPath:filePath forBuildConfiguration:[[[tableView window] windowController] buildConfiguration]] atArrangedObjectIndex:row++];
		}
	}
	return YES;
}

- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
	[pboard declareTypes:[NSArray arrayWithObject:kWCEditIncludeDirectoriesTableViewPboardType] owner:nil];
	[pboard setData:[NSKeyedArchiver archivedDataWithRootObject:rowIndexes] forType:kWCEditIncludeDirectoriesTableViewPboardType];
	return YES;
}
@end
